function P = fsb_get_files_SPM(prefix)

% FSB : function to get files in a SPM format
%
% EXAMPLE:
% P = fsb_get_files_SPM('r')
%
% INPUT:
% prefix:         first letter of files to be loaded
%
% OUTPUT:
% P:              Char array of files compatible with SPM
%
% CALLED BY:
% fsb_prepare_SPM.m
% fsb_realign_SPM.m
% fsb_get_files_SPM.m
%
%
% NOTE:
% This function needs to be called from the directory wherein the files
% reside
%
% Copyright 2010 MPI for Biological Cybernetics
% Author: Steffen Stoewer
% License: GNU GPL, no express or implied warranties
%
%$ Revision 1.0
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

%~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% Check if prefix is provided
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if ~exist('prefix','var');
    prefix = '';
end

%~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% Look for files
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~
thedir = cd;
thedir = [thedir '\'];

all_files = dir([thedir  '\' prefix '*.img']);

if isempty(all_files)
    all_files = dir([thedir  '\' prefix '*.nii']);
end


try
    all_files = vertcat(all_files(:).name);
catch
    disp('Issues with selection of files for SPM in fsb_get_files_SPM.m')
    for x =1:size(all_files,1)
        try
            new_files(x,:)=all_files(x).name;
        catch
            disp(['Error in line ' num2str(x) ' of dir struct']);
        end

    end
    all_files = new_files;
end
all_files = [all_files repmat(',1',size(all_files,1),1)];
P = [repmat(thedir,size(all_files,1),1) all_files];

end
